package com.fortune.service.impl;

import com.fortune.dao.DProductWholesalePriceDao;
import com.fortune.dao.MProductInfoDao;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceAddDto;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceEditDto;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceListDto;
import com.fortune.dto.in.producePriceManagement.EditPriceDealerDto;
import com.fortune.dto.in.producePriceManagement.ProduceDealerListDto;
import com.fortune.service.DProductWholesalePriceService;
import com.fortune.vo.cProductPriceMgt.CProductPrice;
import com.fortune.vo.producePriceManagement.ProductWholesalePriceList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DProductWholesalePriceServiceImpl implements DProductWholesalePriceService {
    @Autowired
    private DProductWholesalePriceDao dProductWholesalePriceDao;
    @Autowired
    private MProductInfoDao mProductInfoDao;

    /**
     * 经销商进货价格列表
     * @param companyId
     * @param produceDealerListDto
     * @return
     */
    @Override
    public List<ProductWholesalePriceList> queryDealerProduceInfo(String companyId, ProduceDealerListDto produceDealerListDto) {
        return dProductWholesalePriceDao.queryDealerProduceInfo(companyId,produceDealerListDto);
    }

    /**
     * 经销商进货价格列表个数
     * @param companyId
     * @param produceDealerListDto
     * @return
     */
    @Override
    public int queryDealerProduceInfoCount(String companyId, ProduceDealerListDto produceDealerListDto) {
        return dProductWholesalePriceDao.queryDealerProduceInfoCount(companyId,produceDealerListDto);
    }

    /**
     * 修改优惠价格
     * @param companyId
     * @param editPriceDealerDto
     * @return
     */
    @Override
    @Transactional
    public int editPrice(String companyId, EditPriceDealerDto editPriceDealerDto) {
        int num = dProductWholesalePriceDao.queryProductWholesaleExist(companyId,editPriceDealerDto);
        if (num < 1){
            num = dProductWholesalePriceDao.addProductWholesale(companyId,editPriceDealerDto);
            if (num < 1){
                throw new RuntimeException("修改优惠价格失败");
            }
        }else {
            num = dProductWholesalePriceDao.queryInProduceIsExist(companyId,editPriceDealerDto);
            if (num > 0){
                num = dProductWholesalePriceDao.deleteInProduce(companyId,editPriceDealerDto);
                if (num < 1){
                    throw new RuntimeException("修改优惠价格失败");
                }
            }
        }
        BigDecimal price = mProductInfoDao.queryProducePrice(companyId,editPriceDealerDto.getProduceId());
        num = dProductWholesalePriceDao.editProductPrice(companyId,editPriceDealerDto);
        if (num < 1){
            throw new RuntimeException("修改优惠价格失败");
        }
        if (price.compareTo(editPriceDealerDto.getPrice()) != 0){
            num = dProductWholesalePriceDao.addInProduce(companyId,editPriceDealerDto);
            if (num < 1){
                throw new RuntimeException("修改优惠价格失败");
            }
        }
        return 1;
    }

    @Override
    public int addProductSalePrice(String companyId,String dealerId, CProductPriceAddDto addDto) {
        return dProductWholesalePriceDao.addProductSalePrice(companyId,dealerId,addDto);
    }

    @Override
    public int editProductSalePrice(String companyId,String dealerId, CProductPriceEditDto editDto) {
        return dProductWholesalePriceDao.editProductSalePrice(companyId,dealerId,editDto);
    }

    @Override
    public int deleteProductSalePrice(String companyId,String dealerId,String productId) {
        return dProductWholesalePriceDao.deleteProductSalePrice(companyId,dealerId,productId);
    }

    @Override
    public List<CProductPrice> findWholesale(String dealerId,CProductPriceListDto cProductPriceListDto) {
        return dProductWholesalePriceDao.findWholesale(dealerId,cProductPriceListDto);
    }

    @Override
    public int findWholesaleCount(String dealerId) {
        return dProductWholesalePriceDao.findWholesaleCount(dealerId);
    }
}
